<?php

namespace App\Admin\Controllers;

use App;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Models\JzNutritionist;
use JzNutritionistService;

class DecreaseNutritionistController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.nutritionist.header.title'));
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            JzNutritionistService::forgetAllName();
            $content->header(trans('backend.nutritionist.header.title'));
            $content->body($this->form()->edit($id));
        });
    }



    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            JzNutritionistService::forgetAllName();
            $content->header(trans('backend.nutritionist.header.title'));
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(JzNutritionist::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('ID')->sortable();
            $grid->nutrition_id(trans('backend.nutritionist.column.nutrition_id'));
            $grid->name(trans('backend.nutritionist.column.name'));
            $grid->type(trans('backend.nutritionist.column.type'))->display(function ($type) {
                return $type == 0 ? '免费流程' : ($type == 1 ? '付费流程' : '其他');
            });
            $grid->sort(trans('backend.nutritionist.column.sort'));
            //$grid->nutrition_info(trans('backend.nutritionist.column.nutrition_info'));
            //$grid->from_appid(trans('backend.nutritionist.column.appid'));
            //$grid->team(trans('backend.nutritionist.column.team'));
            $grid->created_at(trans('backend.nutritionist.column.time'));
            $grid->filter(function($filter){
                $filter->disableId();
                $filter->like('name', trans('backend.nutritionist.column.name'));
                //$filter->like('from_appid', trans('backend.nutritionist.column.appid'));
                //$filter->like('team', trans('backend.nutritionist.column.team'));
            });

            $grid->disableExport();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(JzNutritionist::class, function (Form $form) {
            $form->text('nutrition_id', trans('backend.nutritionist.column.nutrition_id'))->rules('required');
            $form->text('name', trans('backend.nutritionist.column.name'))->rules('required');
            $form->textarea('nutrition_info', trans('backend.nutritionist.column.nutrition_info'))->rules('required');
            $form->select('type', trans('backend.nutritionist.column.type'))->options(['0' => '免费流程', 1 => '付费流程', 2 => '其他'])->default(0);
            $form->number('sort', trans('backend.nutritionist.column.sort'))->rules('required');
        });

    }


}
